package main

import (
	"fmt"
	_ "github.com/GoAdminGroup/go-admin/adapter/iris"
	_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
	_ "github.com/GoAdminGroup/themes/adminlte"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"path/filepath"

	"github.com/GoAdminGroup/go-admin/engine"
	"github.com/GoAdminGroup/go-admin/examples/datamodel"
	"github.com/GoAdminGroup/go-admin/modules/config"
	"github.com/GoAdminGroup/go-admin/plugins/example"
	"github.com/GoAdminGroup/go-admin/template"
	"github.com/GoAdminGroup/go-admin/template/chartjs"
	"github.com/kataras/iris/v12"
)

var config_path = "config.yaml"

func main() {
	app := iris.Default()

	eng := engine.Default()

	parseConfig := func() *config.Config {
		if _, err := os.Stat(config_path); err == nil {
			file, err := os.Open(config_path)
			if err != nil {
				panic(fmt.Sprintln("Open file path:", "./"+config_path, "error:", err))
			}
			defer file.Close()
			all, err := ioutil.ReadAll(file)
			if err != nil {
				panic(fmt.Sprintln("file path:", "./"+config_path, " read all error:", err))
			}
			cfg := config.Config{}
			if err := yaml.Unmarshal([]byte(all), &cfg); err != nil {
				panic(fmt.Sprintln("file path:", "./"+config_path, "json unmarshal error:", err))
			}
			return &cfg
		} else {
			// 获取当前执行
			//文件绝对路径
			getCurrentAbPathByExecutable := func() string {
				exePath, err := os.Executable()
				if err != nil {
					log.Fatal(err)
				}
				res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
				return res
			}
			panic(fmt.Sprintf("error: config file path: %s, config.yml not exist\n", getCurrentAbPathByExecutable()+"/"+config_path))
		}
	}

	//cfg := config.Config{
	//	Env: config.EnvLocal,
	//	Databases: config.DatabaseList{
	//		"default": {
	//			Host:       "127.0.0.1",
	//			Port:       "3306",
	//			User:       "root",
	//			Pwd:        "root",
	//			Name:       "godmin",
	//			MaxIdleCon: 50,
	//			MaxOpenCon: 150,
	//			Driver:     config.DriverMysql,
	//		},
	//	},
	//	UrlPrefix: "admin",
	//	Store: config.Store{
	//		Path:   "./uploads",
	//		Prefix: "uploads",
	//	},
	//	IndexUrl: "/",
	//	Debug:    true,
	//	Language: language.CN,
	//}

	cfg := parseConfig()

	template.AddComp(chartjs.NewChart())

	// customize a plugin

	examplePlugin := example.NewExample()

	// load from golang.Plugin
	//
	// examplePlugin := plugins.LoadFromPlugin("../datamodel/example.so")

	// customize the login page
	// example: https://github.com/GoAdminGroup/demo.go-admin.cn/blob/master/main.go#L39
	//
	// template.AddComp("login", datamodel.LoginPage)

	// load config from json file
	//
	// eng.AddConfigFromJSON("../datamodel/config.json")

	if err := eng.AddConfig(cfg).
		AddGenerators(datamodel.Generators).
		AddDisplayFilterXssJsFilter().
		// add generator, first parameter is the url prefix of table when visit.
		// example:
		//
		// "user" => http://localhost:9033/admin/info/user
		//
		AddGenerator("user", datamodel.GetUserTable).
		AddPlugins(examplePlugin).
		Use(app); err != nil {
		panic(err)
	}

	app.HandleDir("/uploads", "./uploads", iris.DirOptions{
		IndexName: "/index.html",
		Compress:  false,
		ShowList:  false,
	})

	// you can custom your pages like:

	eng.HTML("GET", "/admin", datamodel.GetContent)

	go func() {
		_ = app.Run(iris.Addr(":8099"))
	}()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt)
	<-quit
	log.Print("closing database connection")
	eng.MysqlConnection().Close()
}
